package com.zhitong.money.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhitong.money.entity.PSalaryParam;
import com.zhitong.money.exception.AddException;
import com.zhitong.money.exception.DeleteException;
import com.zhitong.money.exception.RepeatException;
import com.zhitong.money.exception.UpdateException;
import com.zhitong.money.mapper.PSalaryParamMapper;
import com.zhitong.money.mapper.PositionMapper;
import com.zhitong.money.services.api.IPSalaryParamService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class PSalaryParamService implements IPSalaryParamService {

    @Autowired
    private PSalaryParamMapper mapper;

    @Autowired
    private PositionMapper positionMapper;

    @Override
    public List<String> findAllPid() throws Exception {
        List<String> allPids = mapper.findAllPids();
        return allPids;
    }

    @Override
    public List<PSalaryParam> findAllPSalaryParam() throws Exception {
        List<PSalaryParam> allPSalaryParam = mapper.findPSalaryParamByMap(null);
        if (allPSalaryParam==null || allPSalaryParam.isEmpty()){
            throw new NullPointerException("findAllPSalaryParam:查询为空");
        }
        return allPSalaryParam;
    }

    @Override
    public PageInfo findAllPSalaryParamByPage(Map<String, Object> map) throws Exception {

        int pageNum=map.containsKey("pageNum")?(int)map.get("pageNum"):1;
        int pageSize=map.containsKey("pageSize")?(int)map.get("pageSize"):5;

        if (!map.get("did").toString().equals("") || !map.get("name").toString().equals("")) {
            //查询条件处理(获取职位id)
            List<String> idsByMap = positionMapper.findIdsByMap(map);
            if(idsByMap!=null && !idsByMap.isEmpty()){
                map.put("pids",idsByMap);
            }else{
                return new PageInfo(null);
            }
        }


        PageHelper.startPage(pageNum,pageSize);
        List<PSalaryParam> allPSalaryParam = mapper.findPSalaryParamByMap(map);
        if (allPSalaryParam==null || allPSalaryParam.isEmpty()){
            throw new NullPointerException("findAllPSalaryParamByPage:查询为空");
        }
        PageInfo pageInfo=new PageInfo(allPSalaryParam);
        return pageInfo;

    }

    @Override
    public PageInfo findAllPSalaryParamByMap(Map<String, Object> map) throws Exception {
        return null;
    }

    @Override
    public PSalaryParam findPSalaryParamById(String id) throws Exception {
        PSalaryParam psalaryParamById = mapper.findPsalaryParamById(id);
        if (psalaryParamById==null){
            throw new NullPointerException("findPSalaryParamById:查询为空");
        }
        return psalaryParamById;
    }

    @Override
    public PSalaryParam findParamByPid(String pid) throws Exception {
        PSalaryParam pSalaryParamByPid = mapper.findPSalaryParamByPid(pid);
        return pSalaryParamByPid;
    }

    @Override
    public void addPSalaryParam(PSalaryParam pSalaryParam) throws Exception {
        PSalaryParam pSalaryParamByPid = mapper.findPSalaryParamByPid(pSalaryParam.getPid());
        if (pSalaryParamByPid!=null){
            throw new RepeatException("该职位已有参数!");
        }
        try {
            mapper.addPSalaryParam(pSalaryParam);
        } catch (Exception e) {
            throw new AddException("addPSalaryParam:添加失败!"+e.getMessage());
        }
    }

    @Override
    public void updatePSalaryParam(PSalaryParam pSalaryParam) throws Exception {
        PSalaryParam pSalaryParamByPid = mapper.findPSalaryParamByPid(pSalaryParam.getPid());
        try {
            mapper.updatePSalaryParam(pSalaryParam);
        } catch (Exception e) {
            throw new UpdateException("updatePSalaryParam:更新失败!"+e.getMessage());
        }
    }

    @Override
    public void delPSalaryParam(String id) throws Exception {
        try {
            mapper.delPSalaryParam(id);
        } catch (Exception e) {
            throw new DeleteException("delPSalaryParam:删除失败"+e.getMessage());
        }
    }

    @Override
    public void delPSalaryByIds(List<String> ids) throws Exception {
        try {
            mapper.delPSalaryByIds(ids);
        } catch (Exception e) {
            throw new DeleteException("delPsalaryByIds:删除失败"+e.getMessage());
        }
    }
}
